package com.lagou.studentsystem.dao.Impl;


import com.lagou.studentsystem.dao.UserDao;
import com.lagou.studentsystem.model.Student;
import com.lagou.studentsystem.model.User;
import com.lagou.studentsystem.util.DBUtil;
import com.lagou.studentsystem.util.DruidUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserDaoImp implements UserDao {

    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;

    @Override
    public User userLogin(User user) {
        try {
            //1.获取连接
            connection = DruidUtils.getConnection();
            //2.编写sql
            String sql = "select * from account where username = ? and password = ?";
            //3.创建PreparedStatement类型的引用
            preparedStatement = connection.prepareStatement(sql);
            //4.向？传值
            preparedStatement.setString(1,user.getUserName());
            preparedStatement.setString(2,user.getPassWord());
            resultSet = preparedStatement.executeQuery();
            //执行成功
            if(resultSet.next()){
                String userName = resultSet.getString("userName");
                String passWord = resultSet.getString("passWord");
                User tu = new User();
                tu.setUserName(userName);
                tu.setPassWord(passWord);
                return tu;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            try {
                DruidUtils.close(connection,preparedStatement,resultSet);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //执行失败
        return null;
    }


}

